<template>
  <div>
    <!--创建商品分类对话框 -->
    <el-dialog title="创建商品分类" v-model="dialogVisible" :close-on-click-modal="false" @close="resetForm">
      <el-form :model="form" :rules="rules" ref="createForm">
        <el-form-item label="分类名称" prop="name" label-width="80px">
          <el-input v-model="form.name" placeholder="请商品分类名称"></el-input>
        </el-form-item>
        <el-form-item label="分类级别" prop="level" label-width="80px">
          <el-radio-group v-model="form.level">
            <el-radio-button label="一级" value="1"/>
            <el-radio-button label="二级" value="2"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.level==='2'" label="父分类" prop="parentId" label-width="80px">
          <el-select placeholder="请选择父分类" v-model="form.parentId">
            <el-option label="无" value="0"/>
            <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="分类描述" prop="desc" label-width="80px">
          <el-input
              type="textarea"
              v-model="form.desc"
              placeholder="请输入分类描述"
              :rows="4"
              resize="none"
          ></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer">
				<el-button @click="resetForm">重填</el-button>
				<el-button type="primary" @click="submitForm">创建</el-button>
			</span>
    </el-dialog>
  </div>
</template>
<script setup>
defineProps({
  categories: {
    type: Array,
    default: () => [],
    required: true
  }
})
import {ElMessage} from 'element-plus';
import {
  reactive,
  toRefs,
  ref
} from 'vue';
//引入apiUtils
import apiUtils from '@/assets/js/api-utils.js';

const data = reactive({
  dialogVisible: false, // 控制对话框的显示状态
  //表单字段
  form: {
    name: '',
    level: '1',
    parentId: '0',
    desc: ''
  },
  //表单验证规则
  rules: {
    name: [{required: true, message: '请输入分类名称', trigger: 'blur'}],
    level: [{required: true, message: '请选择分类级别', trigger: 'change'}],
    parentId: [{required: true, message: '请选择父分类', trigger: 'change'}],
  }
});
//创建教师表单组件实例
let createForm = ref(null);
let {
  dialogVisible,
  form,
  rules
} = toRefs(data);
//提交form表单
const submitForm = () => {
  createForm.value.validate(async (valid) => {
    if (valid) {
      await createCategory()
    } else {
      console.log('表单验证失败!');
      return false;
    }
  });
};
const createCategory = async () => {
  let params = {...form.value};
  if ('1' === params.level) {
    delete params.parentId;
  }
  console.info(params)
  let response = await apiUtils.category.create(params);
  if (200 !== response.code) {
    ElMessage.error(response.message);
    return;
  }
  ElMessage({message: '创建成功', type: 'success',});
  data.dialogVisible = false;
}

//重置表单
const resetForm = () => {
  createForm.value.resetFields();
};
//暴露dialogVisible,父组件可以直接使用
defineExpose({
  dialogVisible,
});
</script>


<style scoped>
/* 样式可以根据需要自定义 */
</style>